package org.hylexus.hadoop.hadoop_2_test.common;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;

public class HDFSUtil {

	public static FileSystem getFS() {
		return getFS("/");
	}

	public static FileSystem getFS(String path) {
		try {
			///////////////////////////////////////////
			File workaround = new File(".");
			System.getProperties().put("hadoop.home.dir", workaround.getAbsolutePath());
			new File("./bin").mkdirs();
			new File("./bin/winutils.exe").createNewFile();
			///////////////////////////////////////////

			URI uri = new URI("hdfs://hadoop:9000" + path);
			Configuration conf = new Configuration();
			return FileSystem.get(uri, conf, "root");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}
}
